<!-- 立项议题已通过弹框也是此弹框 做条件判断显示or隐藏 -->
<template>
  <div>
    <el-dialog
      v-model="state.centerDialogVisible"
      :title="state.type + '项目议题请示 '"
      width="70%"
      align-center
      @close="handleClose"
      @open="openDialog"
      class="dialogStyle"
    >
      <el-form
        :model="formInline"
        ref="formRef"
        :rules="state.type==='查看'?'':rules"
        class="demo-form-inline"
        label-width="160px"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="议题名称" class="" prop="meetingName">
              <el-input
                :disabled="state.disabled"
                v-model="formInline.meetingName"
                placeholder="议题名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目立项部门" class="">
              <el-input
                v-model="formInline.projectDepartment"
                disabled
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="项目来源" class="" prop="">
              <el-input v-model="formInline.projectSource" placeholder="" />
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item label="项目负责人" class="">
              <el-input
                v-model="formInline.responsibleUserName"
                disabled
                placeholder=""
              />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="关联项目集" class="">
              <el-input
                v-model="formInline.projectSetName"
                disabled
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="关联项目" class="">
              <el-input
                v-model="formInline.projectName"
                disabled
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目集编码" class="">
              <el-input
                v-model="formInline.projectSetCode"
                disabled
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属项目" class="">
              <el-input
                v-model="formInline.projectName"
                disabled
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目编码" class="">
              <el-input
                v-model="formInline.projectCode"
                disabled
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="type == 3">
            <el-form-item label="项目类型" class="">
              <el-select
                v-model="formInline.projectCategory"
                clearable
                disabled
                placeholder=""
              >
                <el-option label="公司自建" value="1" />
                <el-option label="政府金额" value="2" />
                <el-option label="代建项目" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目分类">
              <el-input
                v-model="formInline.projectType"
                disabled
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="业务单元">
              <el-select
                v-model="formInline.businessUnit"
                disabled
                placeholder="业务单元"

              >
                <el-option
                  v-for="item in businessUnitArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计划实施时间范围">
              <el-date-picker
                disabled
                v-model="formInline.predictStartTime"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="拟计划总投额">
              <el-input
                v-model="formInline.projectAmount"
                disabled
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="议题性质" class="" prop="meetingNature">
              <el-select
                v-model="formInline.meetingNature"
                value-key=""
                :disabled="state.disabled"
                placeholder="议题性质"
                clearable
                filterable
              >
                <el-option
                  v-for="item in topicOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <!-- <el-col :span="8">
            <el-form-item label="紧急程度" class="" prop="urgency">
              <el-select
                v-model="formInline.urgency"
                value-key=""
                :disabled="state.disabled"
                placeholder="紧急程度"
                clearable
                filterable
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item label="会审部门" class="" prop="reviewDepartment">
              <el-select
                v-model="formInline.reviewDepartment"
                value-key=""
                :disabled="state.disabled"
                placeholder="会审部门"
                clearable
                collapse-tags
                collapse-tags-tooltip
                multiple
                filterable
              >
                <el-option
                  v-for="item in state.departmentOptions"
                  :key="item.departmentId"
                  :label="item.departmentName"
                  :value="item.departmentId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="议题类型" class="" prop="meetingType">
              <el-select
                v-model="formInline.meetingType"
                value-key=""
                :disabled="state.disabled"
                placeholder="议题类型"
                clearable
              >
                <el-option value="-540387384087627251" label="招标项目">
                </el-option>
                <el-option value="65973154795690558" label="投资项目">
                </el-option>
                <el-option value="96033043598817727" label="建设项目">
                </el-option>
                <el-option value="-2375352083915127" label="其他项目">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="是否人事议题" class="" prop="personnelIssue">
              <el-select
                clearable
                :disabled="state.disabled"
                v-model="formInline.personnelIssue"
              >
                <el-option :value="1" label="是"> </el-option>
                <el-option :value="0" label="否"> </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="议题简述" class="" prop="meetingResume">
              <el-input
                :disabled="state.disabled"
                v-model="formInline.meetingResume"
                placeholder="议题简述"
                type="textarea"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="议题附件" class="" prop="voList">
              <FileMinioUpload
                :restriction="true"
                :restrictionLimit="9"
                @updateFiles="updateFiles"
                :businessType="37"
                :associationId="'1'"
                :disabled="state.disabled"
                v-model:files="formInline.voList"
              />
              <!-- 文件限制9个 -->
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="预设项目阶段" class="">
              <el-tree
                :data="state.dataSource"
                node-key="projectStageId"
                default-expand-all
                :expand-on-click-node="false"
              >
                <template #default="{ node, data }">
                  <span class="custom-tree-node">
                    <span>{{ data.stageName }}</span>
                  </span>
                </template>
              </el-tree>
            </el-form-item>
          </el-col>
          <p v-if="state.disabled">审批结果</p>
          <el-col :span="24" v-if="state.disabled">
            <el-form-item label="审批结果">
              <el-select
                disabled
                v-model="formInline.examineStatus"
                placeholder=""
              >
                <el-option value="1" label="审核中"> </el-option>
                <el-option value="2" label="审核通过"> </el-option>
                <el-option value="3" label="审核驳回"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="state.disabled">
            <el-form-item label="查看审批流程">
              <el-button type="primary" @click="openHandler(formInline.routeUrl)" class="button_edit">查看审批流程</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="state.disabled">
            <el-form-item label="创建时间">
              <el-input disabled v-model="formInline.createTime" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="state.disabled">
            <el-form-item label="项目创建人">
              <el-input disabled v-model="formInline.createName" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer" v-if="!state.disabled">
          <el-button @click="state.centerDialogVisible = false" class="button_cancel2">取消</el-button>
          <el-button @click="bfProjectSubjectMeetingHandel" class="button_view">
            送审
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import FileMinioUpload from "@/components/FileMinioUpload.vue";
import { businessUnitArr } from '@/common/dict/dictCollect.js'

import { projectPhase, department } from '@/api/project.js'
import { ref, reactive, onMounted } from 'vue'
import {
  ElDialog,
  ElSelect,
  ElOption,
  ElForm,
  ElFormItem,
  ElRadio,
  ElRadioGroup,
  ElMessage,
  ElRow,
  ElCol
} from 'element-plus'
import { projectConfigApi, publicProject } from '@/api/project'
const props = defineProps({
  type: {
    type: String,
    default: ''
  }
})
const options = [
  { value: '1', label: '普通' },
  { value: '2', label: '加急' },
  { value: '3', label: '平急' },
  { value: '4', label: '特急' },
  { value: '5', label: '特提' }
]

const topicOptions = [
  { value: '0', label: '三重一大' },
  { value: '1', label: '非三重一大' }
]
let projectTypeList = reactive([])

const rules = {
  projectSource: [
    { required: true, message: '请选择项目来源', trigger: 'blur' }
  ],
  meetingResume: [
    { required: true, message: '请选择议题简述', trigger: 'blur' }
  ],
  projectType: [{ required: true, message: '请选择项目类型', trigger: 'blur' }],
  reviewDepartment: [
    { required: true, message: '请选择会审部门', trigger: 'blur' }
  ],
  urgency: [{ required: true, message: '请选择紧急程度', trigger: 'blur' }],
  personnelIssue: [
    { required: true, message: '请选择是否人事议题', trigger: 'blur' }
  ],
  meetingType: [{ required: true, message: '请选择议题类型', trigger: 'blur' }],
  meetingName: [{ required: true, message: '请选择议题名称', trigger: 'blur' }],
  voList: [{ required: true, message: '请选择议题附件', trigger: 'blur' }],
  meetingNature: [
    { required: true, message: '请选择议题性质', trigger: 'blur' }
  ]
}

// 上传的图片附件
const updateFiles = (list) => {
  if (list.length > 0) {
    console.log(list)
  }
}
const getpublicProject = () => {
  publicProject.getProjectTypeVo('').then((res) => {
    if (res.code == 200) {
      projectTypeList = res.data
      console.log(projectTypeList)
    }
  })
}
let state = reactive({
  centerDialogVisible: false,
  disabled: false,
  departmentOptions: [],
  projectId: '',
  projectTypeId: '',
  dataSource: [], //树
  typeList: [],
  tabId:''
})

const formInline = reactive({
  reviewDepartment: '',
  meetingName: '',
  meetingResume: '',
  meetingNature: '',
  urgency: '',
  personnelIssue: 0,
  voList: [],
  predictStartTime: [],
  files: []
})
// 状态 1 待审核 2审核成功 3审核驳回
let meetingApplyStatus = null

const getBfProjectSubjectMeetingView = (row) => {
  meetingApplyStatus = row.meetingApplyStatus
  projectConfigApi.bfProjectSubjectMeetingView(row.projectId).then((res) => {
    if (res.code == 200) {
      for (let key in res.data) {
        if (res.data[key] !== null) {
          formInline[key] = res.data[key]
        }
        if((key==='reviewDepartment' && res.data[key] ==='' && state.tabId==='1') || (key==='reviewDepartment' && res.data[key] ===null && state.tabId==='1')){
            formInline[key] = ["2960838433294208616"]
        }
        if((key==='projectCode' && res.data[key] ==='' && state.tabId==='1') || (key==='projectCode' && res.data[key] ===null && state.tabId==='1')){
            formInline[key] = res.data['projectId'];
        }
        if (key == 'predictStartTime') {
          formInline[key] = [formInline[key], res.data['predictEndTime']]
          console.log(formInline[key])
        }
        formInline.projectId = row.projectId
      }
    }
  })
}
const formRef = ref(null)
const emits = defineEmits(['refreshList'])
const bfProjectSubjectMeetingHandel = () => {
  console.log(formInline)
  let url = 'bfProjectSubjectMeetingAdd'

  formRef.value.validate((valid, fields) => {
    if (valid) {
      if (meetingApplyStatus == 1) {
        url = 'bfProjectSubjectMeetingAdd'
      } else if (meetingApplyStatus == 3) {
        url = 'bfProjectSubjectMeetingEdit'
      }

      projectConfigApi[url](formInline).then((res) => {
        if (res.code == 200) {
          state.centerDialogVisible = false
          ElMessage.success('操作成功!')
        }
      })
    }
  })
}
//获取部门信息
const getprojectPhaseList = () => {
  state.dataSource = []
  let obj = {
    projectTypeId: state.projectTypeId,
    projectId: state.projectId
  }
  projectPhase.getProjectPhase(obj).then((res) => {
    const { data, code, msg } = res
    if (code == 200) {
      state.dataSource = data.stages
    }
  })
}
//获取所有部门信息
const getdepartmentList = () => {
  department.getAllDepartments().then((res) => {
    state.departmentOptions = res?.data || []
  })
}
const openHandler = (url) => {
  window.open(url)
}
const handleClose = () => {
  state.centerDialogVisible = false
  for (let key in formInline) {
    if (key == 'files' || key == 'voList' || key == 'predictStartTime') {
      formInline[key] = []
    } else {
      formInline[key] = null
    }
  }
  emits('refreshList')
}

const openDialog = () => {
  getprojectPhaseList()
}

onMounted(() => {
  getpublicProject()
  getdepartmentList()
})

defineExpose({
  state,
  formInline,
  getBfProjectSubjectMeetingView
})
</script>
<style lang="scss" scoped>
.el-dialog__body .el-form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}
.el-tree {
  min-width: 150px;
}
.el-form-item {
  width: 100% !important;
}
</style>
<style lang="scss" scoped>
@import '@/views/constructionProjectManage/static/dialog.scss';
</style>
